package com.kuang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JdbcController {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     *  查询全部user信息
     * @return
     */
    @GetMapping("/userList")
    public List<Map<String,Object>> getUserList(){
        String sql ="SELECT * FROM `user`";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @GetMapping("/addUser")
    public String addUser(){
        String sql ="INSERT INTO `user`(`id`,`name`,`pwd`) VALUE('6','eee','eee666')";
        jdbcTemplate.update(sql);
        return "新增成功!!!";
    }

    @GetMapping("/updUser/{id}")
    public String updUser(@PathVariable("id") Integer id){
        String sql = "UPDATE `user` SET `name` = ? ,`pwd` = ?  WHERE `id` = ?";
        Object[]  objects = new Object[3];

        objects[0] = "sakura";
        objects[1] = "0717";
        objects[2] = id;

        jdbcTemplate.update(sql,objects);
        return "修改用户信息成功";
    }

    @GetMapping("/delUser/{id}")
    public String delUser(@PathVariable("id")Integer id){
        String sql = "DELETE FROM `user` WHERE `id` = ?";
        jdbcTemplate.update(sql,id);
        return "删除用户成功!!!";
    }

}
